package com.ruoyi.bpm.service.definition.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.bpm.controller.admin.definition.vo.group.BpmUserGroupPageReqVO;
import com.ruoyi.bpm.controller.admin.definition.vo.group.BpmUserGroupSaveReqVO;
import com.ruoyi.bpm.domain.definition.BpmUserGroup;
import com.ruoyi.bpm.mapper.definition.BpmUserGroupMapper;
import com.ruoyi.bpm.service.definition.BpmUserGroupService;
import com.ruoyi.bpm.utils.BeanUtils;
import com.ruoyi.common.core.enums.CommonStatusEnum;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.ruoyi.bpm.domain.definition.table.BpmUserGroupTableDef.BPM_USER_GROUP;
import static com.ruoyi.bpm.enums.ErrorCodeConstants.USER_GROUP_IS_DISABLE;
import static com.ruoyi.bpm.enums.ErrorCodeConstants.USER_GROUP_NOT_EXISTS;
import static com.ruoyi.common.core.utils.ServiceExceptionUtil.exception;
import static com.ruoyi.common.core.utils.CollectionUtils.convertMap;

/**
 * 用户组 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BpmUserGroupServiceImpl extends BaseServiceImpl<BpmUserGroupMapper, BpmUserGroup> implements BpmUserGroupService {
    @Override
    public QueryWrapper query() {
        return super.query().from(BPM_USER_GROUP);
    }

    @Override
    public Long createUserGroup(BpmUserGroupSaveReqVO createReqVO) {
        BpmUserGroup userGroup = BeanUtils.toBean(createReqVO, BpmUserGroup.class);
        mapper.insert(userGroup);
        return userGroup.getId();
    }

    @Override
    public void updateUserGroup(BpmUserGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateUserGroupExists(updateReqVO.getId());
        // 更新
        BpmUserGroup updateObj = BeanUtils.toBean(updateReqVO, BpmUserGroup.class);
        super.updateById(updateObj);
    }

    @Override
    public void deleteUserGroup(Long id) {
        // 校验存在
        this.validateUserGroupExists(id);
        // 删除
        mapper.deleteById(id);
    }

    private void validateUserGroupExists(Long id) {
        if (getUserGroup(id) == null) {
            throw exception(USER_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public BpmUserGroup getUserGroup(Long id) {
        return super.getById(id);
    }

    @Override
    public List<BpmUserGroup> getUserGroupList(Collection<Long> ids) {
        return super.listByIds(ids);
    }


    @Override
    public List<BpmUserGroup> getUserGroupListByStatus(Integer status) {
        return super.list(query().where(BPM_USER_GROUP.STATUS.eq(status, status != null)));
    }

    @Override
    public TableDataInfo<BpmUserGroup> getUserGroupPage(BpmUserGroupPageReqVO pageReqVO) {
        QueryWrapper queryWrapper = query()
            .where(BPM_USER_GROUP.NAME.like(pageReqVO.getName(), StrUtil.isNotBlank(pageReqVO.getName())))
            .and(BPM_USER_GROUP.STATUS.eq(pageReqVO.getStatus(), pageReqVO.getStatus() != null))
            .and(BPM_USER_GROUP.CREATE_TIME.between(pageReqVO.getCreateTime(), ArrayUtil.isNotEmpty(pageReqVO.getCreateTime())))
            .orderBy(BPM_USER_GROUP.ID.desc());
        Page<BpmUserGroup> page = this.pageAs(PageQuery.build(), queryWrapper, BpmUserGroup.class);
        return TableDataInfo.build(page);
    }

    @Override
    public void validUserGroups(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得用户组信息
        List<BpmUserGroup> userGroups = super.listByIds(ids);
        Map<Long, BpmUserGroup> userGroupMap = convertMap(userGroups, BpmUserGroup::getId);
        // 校验
        ids.forEach(id -> {
            BpmUserGroup userGroup = userGroupMap.get(id);
            if (userGroup == null) {
                throw exception(USER_GROUP_NOT_EXISTS);
            }
            if (CommonStatusEnum.ENABLE != CommonStatusEnum.parse(userGroup.getStatus())) {
                throw exception(USER_GROUP_IS_DISABLE, userGroup.getName());
            }
        });
    }

}
